package com.chen.hotel.controller;

import com.chen.hotel.Service.TypeService;
import com.chen.hotel.VO.ResultVO;
import com.chen.hotel.annotation.LoginToken;
import com.chen.hotel.domain.Hotel;
import com.chen.hotel.domain.Type;
import com.chen.hotel.utils.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    private TypeService typeService;

    /**
     * 创建一个房间类型
     * @param type
     * @return
     */
    @PostMapping("")
    @LoginToken
    public ResultVO create(@RequestBody Type type){
        return ResultVOUtil.success(this.typeService.create(type));
    }

    /**
     * 获取所有的房间类型列表
     * @return
     */
    @GetMapping("/list")
    public ResultVO findAll(@RequestParam(value = "page", defaultValue = "0") Integer page,
                            @RequestParam(value = "size", defaultValue = "10") Integer size){
        PageRequest request = PageRequest.of(page, size);
        Page<Type> typeList = this.typeService.findAll(request);
        return ResultVOUtil.success(typeList);
    }

    /**
     * 更新房间类型
     * @param id
     * @param type
     * @return
     */
    @PatchMapping("/{id}")
    @LoginToken
    public ResultVO update(@PathVariable Integer id, @RequestBody Type type){
        type.setId(id);
        return ResultVOUtil.success(this.typeService.update(type));
    }

    /**
     * 删除房间类型
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @LoginToken
    public ResultVO delete(@PathVariable Integer id){
        this.typeService.delete(id);
        return ResultVOUtil.success(null);
    }
}
